const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin') // 它负责克隆您定义的任何其他规则，并将它们应用于.vue文件中的相应语言块
// . 参考官方文档 https://vue-loader.vuejs.org/migrating.html#a-plugin-is-now-required
// . Vue-loader在15.*之后的版本都是 vue-loader的使用都是需要伴生 VueLoaderPlugin的,
const { CleanWebpackPlugin } = require('clean-webpack-plugin') // 新用法
const HtmlWebpackPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const env = process.argv[3]
module.exports = {
  mode: 'none',
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, './dist'),
    filename: '[name]-[chunkhash:8].js'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: 'vue-loader' // 注意vue-loader vue-template-compiler版本必须一致
      },
      {
        enforce: 'pre', // 提高优先级
        test: /\.(js|vue)/,
        include: path.resolve(__dirname, '../src'),
        use: ['eslint-loader']
      },
      {
        test: /\.js$/,
        use: 'babel-loader',
        include: path.resolve(__dirname, './src')
      },
      {
        test: /\.css$/,
        use: [
          env === 'development' ? 'style-loader' : MiniCssExtractPlugin.loader,
          'css-loader'
        ] // 使用MiniCssExtractPlugin.loader不会触发热重载
      },
      {
        test: /\.less$/,
        use: [
          env === 'development' ? 'style-loader' : MiniCssExtractPlugin.loader,
          'css-loader',
          'postcss-loader',
          'less-loader'
        ]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 8192,
          esModule: false, // 不添加可能出现图片地址是[object%20Module]
          name: 'assets/[name]-[contenthash:8].[ext]'
        }
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      title: 'rd平台',
      template: './public/index.html', // 源模板文件
      filename: 'index.html', // 输出文件【注意：这里的根路径是module.exports.output.path】
      showErrors: true,
      inject: 'body'
    }),
    // make sure to include the plugin for the magic
    new VueLoaderPlugin(),
    new webpack.DefinePlugin({
      // 全局变量
      BASE_URL: JSON.stringify('')
    }),
    new MiniCssExtractPlugin({
      filename: 'css/style.css' // 从 .js 文件中提取出来的 .css 文件的名称
    })
  ]
}
